package com.heima.homeservicemanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.homeservicemanagementsystem.entity.SysPermission;
import com.heima.homeservicemanagementsystem.entity.vo.RoleAuthority;
import com.heima.homeservicemanagementsystem.entity.vo.RoleListVo;
import com.heima.homeservicemanagementsystem.mapper.SysRoleMapper;
import com.heima.homeservicemanagementsystem.entity.SysRole;
import com.heima.homeservicemanagementsystem.mapper.SysUserMapper;
import com.heima.homeservicemanagementsystem.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 角色表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2025-02-27 15:15:14
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<String> getPermissions(String roleId) {
        return baseMapper.getPermissions(roleId);
    }

    @Override
    public boolean check(RoleAuthority authority) {
//        if (authority.isCheck()) {
        baseMapper.deleteCheck(authority);
        return baseMapper.insertCheck(authority);
//        }

//        return baseMapper.deletentoCheck(authority);


    }

    @Override
    public List<RoleListVo> search(String role, String value) {
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(role != null && role != "", SysRole::getId, role).eq(value != null, SysRole::getStatus, value);
        List<SysRole> sysRoles = baseMapper.selectList(lqw);
        List<RoleListVo> roleListVos = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            RoleListVo roleListVo = new RoleListVo();

            roleListVo.setName(sysRole.getCode());
            List<SysPermission> permissions = userMapper.ressyspressionByid(sysRole.getId());
            roleListVo.setPermissions(permissions);
            roleListVo.setId(sysRole.getId());
            roleListVo.setStatus(sysRole.getStatus());
            roleListVo.setCode(sysRole.getName());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysRole.getCreateTime());
            long timeInMillis = calendar.getTimeInMillis();
            roleListVo.setCreateTime(timeInMillis);
//            BeanUtils.copyProperties(sysRole, roleListVo);
            log.info("信息;{}", roleListVo);
            roleListVos.add(roleListVo);
        }

        log.info("获取角色的信息;{}", sysRoles);
        return roleListVos;
    }

    @Override
    public List<RoleListVo> getTableRoles() {
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();

        List<SysRole> sysRoles = this.list();
        List<RoleListVo> roleListVos = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            RoleListVo roleListVo = new RoleListVo();

            roleListVo.setName(sysRole.getCode());
            List<SysPermission> permissions = userMapper.ressyspressionByid(sysRole.getId());
            roleListVo.setPermissions(permissions);
            roleListVo.setId(sysRole.getId());
            roleListVo.setStatus(sysRole.getStatus());
            roleListVo.setCode(sysRole.getName());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysRole.getCreateTime());
            long timeInMillis = calendar.getTimeInMillis();
            roleListVo.setCreateTime(timeInMillis);
//            BeanUtils.copyProperties(sysRole, roleListVo);
            log.info("信息;{}", roleListVo);
            roleListVos.add(roleListVo);
        }

        log.info("获取角色的信息;{}", sysRoles);
        return roleListVos;


    }

    @Override
    public boolean removeMeprebyId(List<String> id) {
        baseMapper.removemeprebyid(id);
        return true;
    }
}

